/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;

import com.eit.toschoolonline.lms.jdo.DocumentItemM;
import com.eit.toschoolonline.lms.service.DocumentItemService;
import com.eit.toschoolonline.lms.service.EntranceItemService;
import com.eit.toschoolonline.lms.service.XmlFileService;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 *
 * @author chanon
 */
public class ZipXMLAction extends DownloadAction {
   
    private List vData;
    private XmlFileService xmlFileService;
    private final static String SUCCESS = "success";
    private Document doc;
    static String path ="c:/TestZip/";
  
    @Override
    protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
           
            String type = arg2.getParameter("type");
            String subjectCode = arg2.getParameter("selectSubject");
            XmlFileService xservice = getXmlFileService();  
            //xservice.createXMLFile(type,subjectCode);
            byte[] dataFile = xservice.createXMLByte(type, subjectCode);
        //Set file
        String contentType = "text/xml";
   
        arg3.setContentType("text/xml");
        arg3.setHeader("Content-disposition", "attachment;filename="+type+".xml" );
        
//        String filePath = path+type+".xml";
//        File file = new File(filePath);
        //return new FileStreamInfo(contentType, file);
        return new ByteArrayStreamInfo(contentType, dataFile);
    }
        
    
  protected class ByteArrayStreamInfo implements StreamInfo {

        protected String contentType;
        protected byte[] bytes;

        public ByteArrayStreamInfo(String contentType, byte[] bytes) {
            this.contentType = contentType;
            this.bytes = bytes;
        }

        public String getContentType() {
            return contentType;
        }

//        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }


    }
     
   
    public XmlFileService getXmlFileService() {
        return xmlFileService;
    }

    public void setXmlFileService(XmlFileService xmlFileService) {
        this.xmlFileService = xmlFileService;
    }
          
}
